package com.jdbc1234.shiro;

import com.jdbc1234.common.ShiroUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;

import java.util.Arrays;


/**
 * 基于权限的访问控制，用户 - 角色 - 权限
 * @author : 章仕烜
 * @create : 2019/12/19
 **/
public class PermissionTest {

//    @Test
    public void testPermitted(){
        Subject currentUser = ShiroUtils
                .login("classpath:shiro_permission.ini",
                        "java1234",
                        "123456");
        System.err.println(currentUser.isPermitted("user:select")?
                "有select的权限":"没有select的权限");
        System.err.println(currentUser.isPermitted("user:update")?
                "有update的权限":"没有update的权限");
        boolean[] results = currentUser.isPermitted(
                "user:add",
                "user:delete",
                "user:select",
                "user:update");
        System.err.println(Arrays.toString(results));
        System.err.println(currentUser.isPermittedAll(
                "user:add",
                "user:delete"
        )?"add、delete权限都有":"add或者delete不全有");
        currentUser.logout();
    }

    public void testCheckPermission(){
        Subject currentUser = ShiroUtils
                .login("classpath:shiro_permission.ini",
                        "java1234",
                        "123456");
        currentUser.checkPermission("user:add");
        try {
            currentUser.checkPermission("user:update");
        } catch (AuthorizationException e) {
            e.printStackTrace();
        }
        try {
            currentUser.checkPermissions(
                    "user:add",
                    "user:delete",
                    "user:select",
                    "user:update");
        } catch (AuthorizationException e) {
            e.printStackTrace();
        }
        currentUser.logout();
    }


    public static void main(String[] args) {
      PermissionTest permissionTest = new PermissionTest();
      permissionTest.testCheckPermission();
    }


}
